package projectmanagement.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import projectmanagement.model.Project;
import projectmanagement.model.dao.ProjectDao;

public class ProjectController extends SimpleFormController {
	public static final String PROJECT_ID = "id";

	private ProjectDao projectManager;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ModelAndView modelAndView = new ModelAndView("project");

		String id = request.getParameter(PROJECT_ID);

		if (id != null && id.trim().length() > 0) {
			Project project = this.projectManager.getProjectById(Integer.parseInt(id));
			
			if (project != null){
				modelAndView.addObject("project", project);
			}else{
				throw new ServletException("Nėra tokio projekto.");
			}
		} else {
			throw new ServletException("Blogai nurodytas id.");
		}

		return modelAndView;
	}

	

	public ProjectDao getProjectManager() {
		return projectManager;
	}



	public void setProjectManager(ProjectDao projectManager) {
		this.projectManager = projectManager;
	}

}
